/*
 * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package top.continew.admin.hanfu.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import top.continew.admin.hanfu.domain.bo.MyBo;
import top.continew.admin.hanfu.domain.dto.MyDto;
import top.continew.admin.hanfu.domain.query.MyQuery;
import top.continew.admin.hanfu.domain.vo.MyVo;
import top.continew.admin.hanfu.service.impl.AboutMeServiceImpl;
import net.dreamlu.mica.core.result.R;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/my")
@RequiredArgsConstructor
public class AboutMeController {

    private final AboutMeServiceImpl aboutMeServiceImpl;

    /**
     * 添加配置数据
     * 
     * @param file
     * @param name
     * @param description
     * @param businessScope
     * @param address
     * @param phone
     * @param status
     * @param updateBy
     * @return
     */
    @PostMapping
    public R addAbout(@RequestParam("file") List<MultipartFile> file,
                      @RequestParam("name") String name,
                      @RequestParam("description") String description,
                      @RequestParam("businessScope") String businessScope,
                      @RequestParam("address") String address,
                      @RequestParam("phone") String phone,
                      @RequestParam("status") Integer status,
                      @RequestParam("user") String updateBy) {
        MyDto myDto = new MyDto();
        myDto.setName(name);
        myDto.setDescription(description);
        myDto.setBusinessScope(businessScope);
        myDto.setAddress(address);
        myDto.setPhone(phone);
        myDto.setStatus(status);
        myDto.setUpdateBy(updateBy);
        myDto.setFile(file);

        if (aboutMeServiceImpl.addAbout(myDto) != 1) {
            System.out.println("添加失败");
            return R.fail("配置添加失败");
        }
        return R.success();
    }

    /**
     * 获取页面
     * 
     * @param myQuery
     * @return
     */
    @PostMapping("/condition")
    public R<Page<MyVo>> condition(@RequestBody MyQuery myQuery) {
        Page<MyVo> condition = aboutMeServiceImpl.condition(myQuery);
        System.out.println(condition.getRecords().toString());
        return R.success(condition);
    }

    /**
     * 根据id组删除对应的数据
     * 
     * @param ids
     * @return
     */
    @DeleteMapping
    public R deleteAbout(@RequestBody ArrayList<Integer> ids) {
        int result = aboutMeServiceImpl.deleteAboutMe(ids);
        return result == 1 ? R.success() : R.fail("删除失败");
    }

    /**
     * 更新配置
     * 
     * @param myDto
     * @return
     */
    @PutMapping
    public R updateAbout(@RequestBody MyDto myDto) {
        return aboutMeServiceImpl.updateAboutMe(myDto) == 1 ? R.success() : R.fail("更新失败");
    }

    /**
     * 更新关于我的配置图片
     * 
     * @param img
     * @param id
     * @param user
     * @param newOssId
     * @return
     */
    @PutMapping("/img")
    public R updateAboutImg(@RequestParam("img") List<MultipartFile> img,
                            @RequestParam("id") Integer id,
                            @RequestParam("user") String user,
                            @RequestParam("ossId") String newOssId) {
        MyBo myBo = new MyBo();
        if (img.get(0).getSize() != 0) {
            myBo.setFile(img);
        }
        myBo.setId(id);
        myBo.setUpdateBy(user);
        myBo.setOssId(newOssId);

        int i = aboutMeServiceImpl.updateAboutMeImg(myBo);

        if (i == 0) {
            return R.fail("更新失败");
        }
        return R.success();
    }

    /**
     * 微信端获取关于我的页面
     * 
     * @return
     */
    @SaIgnore
    @PostMapping("/wx")
    public R<MyVo> gwtAboutWx() {
        MyVo myWxList = aboutMeServiceImpl.getAboutWx();
        return R.success(myWxList);
    }
}
